<template>
  <div>
    <el-card class="login-form-layout">
      <el-form
        v-if="!phoneNumberShow"
        autoComplete="on"
        :model="loginForm"
        :rules="loginRules"
        ref="loginForm"
        label-position="left"
      >
        <div style="text-align: center">
          <svg-icon
            icon-class="login-mall"
            style="width: 56px; height: 56px; color: #409eff; margin-bottom: 20px;"
          ></svg-icon>
        </div>
        <el-form-item prop="username">
          <el-input
            name="username"
            type="text"
            v-model="loginForm.username"
            autoComplete="on"
            placeholder="统一社会信用代码/纳税人识别号"
          >
            <span slot="prefix">
              <svg-icon icon-class="user" class="color-main"></svg-icon>
            </span>
          </el-input>
        </el-form-item>
        <el-form-item prop="numberNumber">
          <el-input
            name="numberNumber"
            type="text"
            v-model="loginForm.numberNumber"
            autoComplete="on"
            placeholder="居民身份证号码/手机号/用户名"
          >
            <span slot="prefix">
              <svg-icon icon-class="user" class="color-main"></svg-icon>
            </span>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            name="password"
            :type="pwdType"
            @keyup.enter.native="handleLogin"
            v-model="loginForm.password"
            autoComplete="on"
            placeholder="请输入密码"
          >
            <span slot="prefix">
              <svg-icon icon-class="password" class="color-main"></svg-icon>
            </span>
            <span slot="suffix" @click="showPwd">
              <svg-icon icon-class="eye" class="color-main"></svg-icon>
            </span>
          </el-input>
        </el-form-item>
        <el-form-item style="margin-bottom: 60px; text-align: center">
          <el-button
            style="width: 100%"
            type="primary"
            :loading="loading"
            @click.native.prevent="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      <div v-if="phoneNumberShow">
        <div @click="goback"><i class="el-icon-arrow-left"></i>返回</div>
        <el-form :model="codeForm">
          <div style="text-align: center">
            <svg-icon
              icon-class="login-mall"
              style="width: 56px; height: 56px; color: #409eff"
            ></svg-icon>
          </div>
          <el-form-item class="mt30">
            <el-input
              name="username"
              disabled
              type="text"
              v-model="codeForm.phone"
              autoComplete="on"
            >
              <span slot="prefix">
                <svg-icon icon-class="user" class="color-main"></svg-icon>
              </span>
            </el-input>
          </el-form-item>
          <el-form-item class="mt30">
            <el-input
              name="username"
              type="text"
              prefix-icon="el-icon-phone"
              v-model="codeForm.code"
              autoComplete="on"
            >
            <template slot="append">
              <el-button
                class="btnSty"
                :disabled="btndisabled"
                type="primary"
                @click="sendCode"
                >{{ btnName }}</el-button
              >
            </template>

            </el-input>
          </el-form-item>
          <el-form-item class="mt30">
            <el-button size="small" style="width: 100%" type="primary">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { isvalidUsername } from "@/utils/validate";
import { setSupport, getSupport, setCookie, getCookie } from "@/utils/support";

export default {
  name: "login",
  data() {

    return {
      loginForm: {
        username: "",
        password: "",
        numberNumber: "",
      },
      codeForm: {
        phone: "15091716704",
        code: "",
      },
      phoneNumberShow: false,
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: '请输入统一社会信用代码/纳税人识别号',},
        ],
        numberNumber: [
          { required: true, trigger: "blur", message: '请输入居民身份证号码/手机号/用户名', },
        ],
        password: [
          { required: true, trigger: "blur",  message: '请输入密码',},
        ],
      },
      loading: false,
      pwdType: "password",
      time: 60,
      btndisabled: false,
    };
  },

  computed: {
    btnName() {
      if (this.time == 0) {
        this.btndisabled = false;
        this.time = 60;
        return "重新发送";
      } else if (this.time > 0 && this.time < 60) {
        this.btndisabled = true;
        return `(${this.time}后重新发送)`;
      } else {
        this.btndisabled = false;
        return `发送验证码`;
      }
    },
  },
  created() {
    this.loginForm.username = getCookie("username");
    this.loginForm.password = getCookie("password");
    if (
      this.loginForm.username === undefined ||
      this.loginForm.username == null ||
      this.loginForm.username === ""
    ) {
      this.loginForm.username = "admin";
    }
    if (
      this.loginForm.password === undefined ||
      this.loginForm.password == null
    ) {
      this.loginForm.password = "";
    }
  },
  methods: {
    goback() {
      this.phoneNumberShow = false;
    },
    timer() {
      var timecount = setInterval(() => {
        this.time--;
        if (this.time < 1) {
          clearInterval(timecount);
        }
      }, 1000);
    },
    sendCode() {
      if (this.codeForm.phone == "") {
        this.$message.error("手机号不能为空");
        return false;
      } else if (
        !/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(
          this.codeForm.phone
        )
      ) {
        this.$message.error("手机号不正确");
        return false;
      } else if (this.codeForm.phone.length != 11) {
        this.$message.error("手机号不正确");
        return false;
      } else {
        //向后台发送请求
        this.timer();
      }
    },
    showPwd() {
      if (this.pwdType === "password") {
        this.pwdType = "";
      } else {
        this.pwdType = "password";
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.codeForm.phone = this.loginForm.numberNumber;
          this.phoneNumberShow = true;
        } else {

          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.mt30 {
  margin-top: 30px;
}
.login-form-layout {
  position: absolute;
  left: 0;
  right: 0;
  width: 500px;
  margin: 140px auto;
}

.login-title {
  text-align: center;
}

.login-center-layout {
  background: #409eff;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  margin-top: 200px;
}
.el-input__inner {
  border-radius: 0 !important;
}
.btnSty {
  width: 150px;
  background-color: #409eff !important;
  color: #fff !important;
  border-radius: 0 !important;
}
 .el-button.is-disabled, .el-button.is-disabled:focus, .el-button.is-disabled:hover {
  background-color: #eee !important;
  color: red !important;
}
</style>
